package com.nanyang.tms.manager.waybill.shipment.handler;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.handler.AbstractTransHandler;
import com.nanyang.tms.common.dict.TmsExceptionEnum;
import com.nanyang.tms.common.exception.TmsManagerException;
import com.nanyang.tms.common.util.TmsExceptionUtil;
import com.nanyang.tms.dao.product.ProductWeightModelDao;
import com.nanyang.tms.dao.saleproduct.SaleProductWeightModelDao;
import com.nanyang.tms.domain.forecast.context.CalculateWeightContext;
import com.nanyang.tms.domain.waybill.shipment.context.CalculateSupplierWeightContext;
import com.nanyang.tms.manager.forecast.handler.weight.AbstractCalculateWeight;
import com.nanyang.tms.manager.waybill.shipment.handler.weight.AbstractCalculateSupplierWeight;
@Component
public class CalculateSupplierWeightHandler extends AbstractTransHandler{
    @Autowired
	private ProductWeightModelDao productWeightModelDao;
	@Override
	public boolean isAsync() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		CalculateSupplierWeightContext calculateSupplierWeightContext=(CalculateSupplierWeightContext) context.getInnerContext(CalculateSupplierWeightContext.class);
		try {
			String weightTypeCode=productWeightModelDao.getWeightTypeCodeByProductCode(calculateSupplierWeightContext.getProductCode());
			if(StringUtils.isNotEmpty(weightTypeCode)&&AbstractCalculateSupplierWeight.calculateWeightMap.get(weightTypeCode)!=null){
				AbstractCalculateSupplierWeight.calculateWeightMap.get(weightTypeCode).process(context);
			}else{
				throw new TmsManagerException(TmsExceptionEnum.WEIGHT_TYPE_CODE_IS_NOT_EXIST.getCode(), TmsExceptionEnum.WEIGHT_TYPE_CODE_IS_NOT_EXIST.getMessage());
			}
			
		} catch (Exception e) {
			throw TmsExceptionUtil.wrapException(e);
		}
		return true;
	}

}
